package com.zjuee.web.warning.filter;

import com.alibaba.fastjson.JSONObject;
import com.zjuee.base.constant.Common;
import com.zjuee.base.dto.common.login.LoginDto;
import com.zjuee.base.dto.common.login.LoginInitDto;
import com.zjuee.base.utils.AESUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;


/**
 * @author wu liang
 * @since 1.0
 */
@Component
public class TokenAnalyzationFilter implements Filter {
    @Resource
    private LoginDto loginDto;

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        String token = req.getHeader(Common.Service.ZJUEE_GATEWAY_AUTH_TOKEN);
        if(token!=null){
            String str = AESUtils.decode("", "",  token);
            LoginInitDto loginDtoInit = JSONObject.parseObject(str, LoginInitDto.class);
            loginDto.setUserId(loginDtoInit.getUserId());
            loginDto.setUserName(loginDtoInit.getUserName());
            loginDto.setOrgBusinessId(loginDtoInit.getBciId());
            loginDto.setOrgName(loginDtoInit.getBciName());
        }

        filterChain.doFilter(req,servletResponse);
    }
}
